
#include <stdio.h>
#include <amanita/aMath.h>
#include "../Main.h"
#include "TarotDeck.h"
#include "Card.h"




TarotDeck::TarotDeck() : CardSet() {
	memset(symbol2trump,0,sizeof(symbol2trump));
	memset(trumpOrder,0,sizeof(trumpOrder));
	memset(trumpElements,0,sizeof(trumpElements));
	memset(cardSigns,0,sizeof(cardSigns));
	title = 0;
	author = 0;
	artist = 0;
	reversed = 0;
	back = 0;
	width = 0;
	height = 0;
}

TarotDeck::~TarotDeck() {
	if(title) { free(title);title = 0; }
	if(author) { free(author);author = 0; }
	if(artist) { free(artist);artist = 0; }
	if(cards) for(int i=0; i<sz; i++) delete cards[i];
}

FILE *TarotDeck::openFile(const char *fn) {
	return app.fopenlangf(0,"tarot/deck.%s.txt",fn);
}

void TarotDeck::readHeader(FILE *fp) {
	const char *s;
	int i,c,n;
	aString str;
	CardSet::readHeader(fp);
	str.appendTab(fp);
	if(title) free(title);
	title = strdup(str.toCharArray());
	str.clear().appendTab(fp);
	if(author) free(author);
	author = strdup(str.toCharArray());
	str.clear().appendTab(fp);
	if(artist) free(artist);
	artist = strdup(str.toCharArray());
	str.clear().appendTab(fp);
	reversed = str.toInt();

	str.clear().appendTab(fp);
	char tru[256];
	char sym[22];
	memset(tru,0,sizeof(tru));
	memset(sym,0,sizeof(sym));
	for(i=0,s=str.toCharArray(); i<22; i++) c = app.occult.charToSymbol(s[i]),tru[c] = i,sym[i] = c;
	str.clear().appendTab(fp);
	memset(symbol2trump,0,sizeof(symbol2trump));
	for(i=0,n=str.length(),s=str.toCharArray(); i<n; i++) {
		c = app.occult.charToSymbol(s[i]);
		symbol2trump[c] = (tarot_card)tru[c];
	}
	for(i=0; i<22; i++) trumpOrder[i] = symbol2trump[(int)sym[i]];

	str.clear().appendTab(fp);
	for(i=0,n=str.length(),s=str.toCharArray(); i<n; i++) trumpElements[i] = (tarot_element)(s[i]-'0');

	str.clear().appendTab(fp);
	for(i=0,n=str.length(),s=str.toCharArray(); i<n; i+=2) {
		cardSigns[i] = s[i];
		cardSigns[i+1] = s[i+1];
	}
}

void TarotDeck::readCards(FILE *fp) {
	aString str;
	Card *c;
	for(int i=0; i<sz; i++) {
		c = cards[i] = new Card(*this,i,0);
		str.clear().appendTab(fp);
		c->setName(str.toCharArray());
		c->setTitle(str.toCharArray());
	}
}

void TarotDeck::loadImages(const char *nm) {
	static const char *images[] = {
		"0","I","II","III","IV","V","VI","VII","VIII","IX","X","XI","XII","XIII","XIV","XV","XVI","XVII","XVIII","XIX","XX","XXI",
		"F1","F2","F3","F4","F5","F6","F7","F8","F9","F10","F11","F12","F13","F14",
		"W1","W2","W3","W4","W5","W6","W7","W8","W9","W10","W11","W12","W13","W14",
		"A1","A2","A3","A4","A5","A6","A7","A8","A9","A10","A11","A12","A13","A14",
		"E1","E2","E3","E4","E5","E6","E7","E8","E9","E10","E11","E12","E13","E14",
		"B"
	};
	int i,n;
	char fn[64];
	Card *c;
	cairo_surface_t *image;
	for(i=0; i<sz; i++) {
		c = cards[i];
		if(c->isMajor()) n = c->getNumber();
		else n = 21+(int)c->getElement()*14+c->getNumber();
		sprintf(fn,"%simages/tarot/%s/%s.png",app.getHomeDir(),nm,images[n]);
		image = app.getSurface(fn);
		c->setImage(image);
	}
	sprintf(fn,"%simages/tarot/%s/%s.png",app.getHomeDir(),nm,images[78]);
	back = app.getSurface(fn);
	width = cairo_image_surface_get_width(back);
	height = cairo_image_surface_get_height(back);
}

qabalah_hebrew TarotDeck::trumpToHebrew(tarot_card t) {
	tarot_card o = trumpOrder[t];
	char s = cardSigns[o*2+1];
	qabalah_hebrew h = Qabalah::charToHebrew(s);
app.printf("TarotDeck::trumpToHebrew(trump=%d,order=%d,s=%c,h=%d)",(int)t,(int)o,s,(int)h);
	return h;
//	return Qabalah::charToHebrew(cardSigns[trumpOrder[t]*2+1]);
}

